export install_path_header=${HOME}/opt-gcc
export compiler_c=gcc
export compiler_cxx=g++
export compiler_fortran=gfortran
export compiler_mpicc=mpicc
export compiler_mpicxx=mpicxx
export source_codes_root_path=$(pwd)
export compile_cores_number=20




# install anaconda3
if [ -d ${install_path_header}/anaconda3 ];then
    echo "anaconda3 has been installed"
else
    bash ./Anaconda3-2021.05-Linux-x86_64.sh -b -p ${install_path_header}/anaconda3
fi


# install mpich3
export CC=${compiler_c}
export CXX=${compiler_cxx}
export FC=${compiler_fortran}
package=mpich-3.2.1
install_path=mpich
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
    cd ${package}
    ./configure --prefix=${install_path_header}/${install_path}
    make -j${compile_cores_number}
    make install
    cd ..
fi
export PATH=${install_path_header}/${install_path}/bin:$PATH



# install hdf5
export CC=${compiler_c}
export CXX=${compiler_cxx}
export FC=${compiler_fortran}
export CFLAGS=-fPIC
package=hdf5-1.12.0
install_path=hdf5
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.bz2
    cd ${package}
    ./configure --prefix=${install_path_header}/${install_path}
    make -j${compile_cores_number}
    make install
    cd ..
fi
export CFLAGS=""

# install hdf5-mpich
export CC=${compiler_mpicc}
package=hdf5-1.12.0
install_path=hdf5-mpich
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.bz2
    cd ${package}
    ./configure --enable-parallel --prefix=${install_path_header}/${install_path}
    make -j${compile_cores_number}
    make install
    cd ..
fi




# install SuperLU
# In CmakeLists.txt file: change set(CMAKE_C_FLAGS "-DPRNTlevel=0 -DAdd_ ${CMAKE_C_FLAGS}")
#                         to     set(CMAKE_C_FLAGS "-Ofast -DAdd_ ${CMAKE_C_FLAGS}")
export CC=${compiler_c}
package=superlu_5.2.1
install_path=superlu
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
    mv SuperLU_5.2.1 ${package}
    cd ${package}
    mkdir build
    cd build
    mkdir ${install_path_header}/${install_path}
    cmake ../ -DCMAKE_INSTALL_PREFIX=${install_path_header}/${install_path}
    make -j${compile_cores_number}
    make install
    if [ -d ${install_path_header}/${install_path/lib64} ];then
        mv ${install_path_header}/${install_path}/lib64 ${install_path_header}/${install_path}/lib
    fi
    cp CBLAS/libblas.a ${install_path_header}/${install_path}/lib/libblas.a
    cd ../..
fi



# install PETSc
export CC=${compiler_c}
export FC=${compiler_fortran}
package=petsc-3.8.3
install_path=petsc
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
    cd ${package}
    python2 ./configure --with-debugging=no --with-mpi-dir=${install_path_header}/mpich --download-fblaslapack=${source_codes_root_path}/fblaslapack-3.4.2_for_petsc-3.8.3.zip --prefix=${install_path_header}/${install_path}
    #python2 ./configure --with-debugging=no --with-mpi-dir=${install_path_header}/mpich  --with-blaslapack-lib=${install_path_header}/OpenBLAS/lib/libopenblas.a --prefix=${install_path_header}/${install_path}

    make  MAKE_NP=${compile_cores_number} all test && make  install
    cd ..
fi



# install metis
export CC=${compiler_c}
package=metis-5.1.0eric
install_path=metis
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
    cd ${package}
    mkdir build
    cd build
    mkdir ${install_path_header}/${install_path}
    cmake ../ -DCMAKE_INSTALL_PREFIX=${install_path_header}/${install_path}
    make -j${compile_cores_number}
    make install
    cd ../..
fi



# install hypre
export CC=${compiler_c}
package=hypre-2.22.0
install_path=hypre
if [ -d ${install_path_header}/${install_path} ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
    cd ${package}
    mkdir build
    cd build
    mkdir ${install_path_header}/${install_path}
    cmake ../src/ -DCMAKE_INSTALL_PREFIX=${install_path_header}/${install_path}
    make -j${compile_cores_number}
    make install
    cd ../..
fi


# install mfem
export CC=${compiler_c}
package=mfem-4.3
install_path=mfem
if [ -d ${install_path_header}/${install_path}11 ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
	tar -xvf ${package}.tgz
    cd ${package}
    mkdir build
    cd build
    mkdir ${install_path_header}/${install_path}
    cmake ../ -DCMAKE_INSTALL_PREFIX=${install_path_header}/${install_path} -DMFEM_USE_MPI=YES -DMFEM_USE_METIS_5=YES -DHYPRE_DIR=${install_path_header}/hypre -DMETIS_DIR=${install_path_header}/metis
    make -j${compile_cores_number}
    make install
    cd ../..
fi


# install cgns
export CC=${compiler_c}
package=CGNS-4.2.0
install_path=cgns
if [ -d ${install_path_header}/${install_path}11 ];then
    echo "${package} has been installed"
else
    tar -xvf ${package}.tar.gz
    tar -xvf ${package}.tar
    tar -xvf ${package}.gz
    cd ${package}
    mkdir build
    cd build
    mkdir ${install_path_header}/${install_path}
    cmake ../ -DCMAKE_INSTALL_PREFIX=${install_path_header}/${install_path}  -DCGNS_ENABLE_HDF5="ON" -DCMAKE_PREFIX_PATH="${install_path_header}/hdf5" -DCGNS_BUILD_CGNSTOOLS="ON"
    make -j${compile_cores_number}
    make install
    cd ../..
fi